# 游戏数据显示类
import pygame
from .. import constants as C
from . import coin
from .. import setup, tools

pygame.font.init()  # pygame游戏字体信息初始化


class Info:
    def __init__(self, state, game_info):
        self.state = state  # 变量state表示游戏阶段信息
        self.game_info = game_info  # 变量game_info表示游戏关键数据信息(生命数、分数等)
        self.create_state_labels()  # 创建某阶段特有的标签文字信息
        self.create_info_labels()  # 创建通用的界面标签信息(金币、分数、时间等)
        self.flash_coin = coin.FlashingCoin()  # 实例化coin.FlashingCoin()类

    def create_state_labels(self):
        # 空列表以存放游戏阶段标签信息(每一个标签元素都有两个参数(标签图片, 放置坐标))
        self.state_labels = []
        if self.state == 'main_menu':
            self.state_labels.append((self.create_label('1 PLAYER GAME'), (272, 360)))
            self.state_labels.append((self.create_label('2 PLAYER GAME'), (272, 405)))
            self.state_labels.append((self.create_label('TOP - '), (290, 465)))
            self.state_labels.append((self.create_label('000000'), (400, 465)))
        elif self.state == 'load_screen':
            self.state_labels.append((self.create_label('WORLD'), (280, 200)))
            # 关卡名标签(为当前关卡名)
            self.state_labels.append((self.create_label('1 - 1'), (430, 200)))
            # 生命数标签(更新为当前阶段的生命数)，f格式化也可写成'x    {}'.format(self.game_info["lives"])
            self.state_labels.append((self.create_label(f'x    {self.game_info["lives"]}'), (380, 280)))
            self.player_image = tools.get_image(setup.GRAPHICS['mario_bros'], 178, 32, 12, 16, (0, 0, 0), C.BG_MULTI)
        elif self.state == 'game_over':
            self.state_labels.append((self.create_label('GAME OVER'), (280, 200)))

    def create_info_labels(self):
        # 空列表以存放游戏界面标签信息(每一个标签元素都有两个参数(标签图片, 放置坐标))
        self.info_labels = []
        self.info_labels.append((self.create_label('MARIO'), (75, 30)))
        self.info_labels.append((self.create_label('WORLD'), (450, 30)))
        self.info_labels.append((self.create_label('TIME'), (625, 30)))
        self.info_labels.append((self.create_label('000000'), (75, 55)))
        self.info_labels.append((self.create_label('x00'), (300, 55)))
        self.info_labels.append((self.create_label('1 - 1'), (480, 55)))

    # 通用标签创建方法,根据传入参数:文字内容尺寸宽高倍率等将其渲染成相应图片
    def create_label(self, label, size=40, width_scale=1.25, height_scale=1):
        # 方法1:字体->文字->图片
        # SysFont()方法从电脑系统获得字体
        font = pygame.font.SysFont(C.FONT, size)
        # render()方法将文字渲染成图片(255, 255, 255)表示白色字体(可调)
        label_image = font.render(label, 1, (255, 255, 255))

        # 以下两行用以现将图片放大以产生锯齿效果
        rect = label_image.get_rect()
        label_image = pygame.transform.scale(label_image,
                                             (int(rect.width * width_scale),
                                              int(rect.height * height_scale)))
        return label_image  # 返回需求标签图片

        # 方法2:文字->抠图->图片:
        # 利用tools.get_image()方法实现(见SuperMario2文件夹)

    def update(self):  # 更新
        self.flash_coin.update()  # 调用金币更新方法更新金币状态

    def draw(self, surface):  # 绘画
        # 遍历游戏阶段标签列表并绘制
        for label in self.state_labels:
            # label[0]为先前制得的标签图片，label[1]为图片绘制坐标
            surface.blit(label[0], label[1])
        # 遍历游戏阶段标签列表并绘制
        for label in self.info_labels:
            # label[0]为先前制得的标签图片，label[1]为图片绘制坐标
            surface.blit(label[0], label[1])
        surface.blit(self.flash_coin.image, self.flash_coin.rect)

        if self.state == 'load_screen':
            surface.blit(self.player_image, (300, 270))
